package com.pzl.dashboardview;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.text.TextPaint;
import android.util.Log;
import android.view.animation.BounceInterpolator;
import android.view.animation.LinearInterpolator;

/**
 * Created by zl.peng on 2016-02-02.
 */
public class SimpleProgress extends BaseDialPlate {
    private static final String TAG = "SimpleDialPlate";
    private Paint mPaint;
    private int mScaleStrokeWidth = 15;
    private int mPointStrokeWidth = 15;
    private int mBgColor = Color.parseColor("#ffffffff");
    private int mPointBgColor = Color.parseColor("#ffFFAA22");
    private int mBaseColor = Color.parseColor("#55ff0000");
    private int mPointColor = Color.parseColor("#ff86b94f");
    private int mTextColor = Color.parseColor("#ff2b2b2b");
    private int mTextSize = 50;
    private int mCx, mCy, mR;
    private float mCurrentAngle = 0;
    private String mCurrentValue = "";

    /**
     * 刻度长度
     */
    private int mScaleLength = 3;
    /**
     * 是否有表盘刻度
     */
    private boolean mHasScale = true;
    private RectF mRectF;
    private DashboardView mDashboardView;
    private ValueAnimator mValueAnimator_Progress, mValueAnimator_Text;
    private int mAnimDuration = 3000;
    private Paint mTextPaint;

    public SimpleProgress() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mRectF = new RectF();
        mTextPaint = new TextPaint();
    }


    @Override
    public void onDraw(DashboardView dashboardView, Canvas canvas) {
        Log.i(TAG, "onDraw");
        super.onDraw(dashboardView, canvas);

        reset();
        initParams();
        mDashboardView = dashboardView;

        if (mHasScale && mSegmentNum != 0) {
            drawScale(canvas);
        }

        drawPoint(canvas);
        drawText(canvas);
    }

    /**
     * 画提示文本
     *
     * @param canvas
     */
    private void drawText(Canvas canvas) {
        Rect rect = new Rect();
        mTextPaint.getTextBounds(mCurrentValue, 0, mCurrentValue.length(), rect);
        int w = rect.width();
        int h = rect.height();
        int x = mCx - w / 2;
        int y = mCy - h / 2;
        mTextPaint.setColor(mTextColor);
        mTextPaint.setTextSize(mTextSize);


        canvas.drawText(mCurrentValue, x, y, mTextPaint);
    }

    /**
     * 画刻度
     *
     * @param canvas
     */
    private void drawScale(Canvas canvas) {
        canvas.save();

        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mScaleStrokeWidth);
        mPaint.setColor(mBaseColor);

        for (int i = 0; i <= mSegmentNum - 1; i++) {
            canvas.save();
            canvas.drawLine(mCx, (mCy + mR) - mPointStrokeWidth, mCx, ((mCy + mR) - mPointStrokeWidth) - mScaleLength, mPaint);
            canvas.restore();
            canvas.rotate((360f / mSegmentNum), mCx, mCy);
        }
        canvas.restore();
    }

    /**
     * 画指针
     *
     * @param canvas
     */
    private void drawPoint(Canvas canvas) {
        mPaint.setStyle(Paint.Style.STROKE);
        mRectF.set((0 + mPointStrokeWidth / 2) + (mWidth / 2 - mMinSize / 2), (0 + mPointStrokeWidth / 2) + (mHeight / 2 - mMinSize / 2), (mWidth - mPointStrokeWidth / 2) - (mWidth / 2 - mMinSize / 2), (mHeight - mPointStrokeWidth / 2) - (mHeight / 2 - mMinSize / 2));

        mPaint.setStrokeWidth(mPointStrokeWidth);
        mPaint.setColor(mPointBgColor);
        canvas.drawOval(mRectF, mPaint);
        mPaint.setColor(mPointColor);
        canvas.drawArc(mRectF, 90, mCurrentAngle, false, mPaint);
    }

    private void initParams() {
        mCx = mWidth / 2;
        mCy = mHeight / 2;
        mR = mMinSize / 2;
    }

    /**
     * ondraw()可能会被调用多次
     */
    private void reset() {
    }

    @Override
    public void setStaticValue(int staticValue) {
        super.setStaticValue(staticValue);
        initProgressAnim(staticValue);
        initTextAnim(staticValue);
        mValueAnimator_Text.start();
        mValueAnimator_Progress.start();
    }

    /**
     * 配置进度条动画
     *
     * @param staticValue
     */
    private void initProgressAnim(int staticValue) {
        float rat = (float) staticValue / (float) mMaxValue;
        float become = rat * 360f;
        Log.i(TAG, "become=" + become);
        mValueAnimator_Progress = ValueAnimator.ofFloat(0f, become);
        mValueAnimator_Progress.setDuration(mAnimDuration);
        mValueAnimator_Progress.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mCurrentAngle = (float) animation.getAnimatedValue();
                mDashboardView.postInvalidate();
                Log.i(TAG, "mCurrentAngle=" + mCurrentAngle);
            }
        });
        mValueAnimator_Progress.setInterpolator(new BounceInterpolator());
        mValueAnimator_Progress.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                mValueAnimator_Progress.removeAllUpdateListeners();
                mValueAnimator_Progress.removeAllListeners();
                mValueAnimator_Progress = null;
            }
        });
    }

    /**
     * 配置文本动画
     *
     * @param staticValue
     */
    private void initTextAnim(int staticValue) {
        mValueAnimator_Text = ValueAnimator.ofInt(0, staticValue);
        mValueAnimator_Text.setDuration(mAnimDuration);
        mValueAnimator_Text.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mCurrentValue = Integer.toString((int) animation.getAnimatedValue());
            }
        });
        mValueAnimator_Text.setInterpolator(new BounceInterpolator());
        mValueAnimator_Text.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                mValueAnimator_Text.removeAllUpdateListeners();
                mValueAnimator_Text.removeAllListeners();
                mValueAnimator_Text = null;
            }
        });
    }

    @Override
    public void onUpdate(int currentValue) {
        float rat = (float) currentValue / (float) mMaxValue;
        mCurrentAngle = rat * 360f;
    }
}
